/**
 *  Copyright 2010 ShenYang voole Co. Ltd.
 *  All right reserved. 
 */
package com.wm.dao.hibernate;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.wm.bean.City;
import com.wm.comm.Constant;
import com.wm.dao.CityDAO;

/**
 * 功能描述:城市数据操作
 *
 * 创建日期 Dec 29, 2012
 *
 * @author zhangbaochao
 *
 * @version 1.0
 *
 */
public class CityHibernateDAO extends HibernateDaoSupport implements CityDAO{
	
	private static final String LOAD_BY_ID = "from City where id = ?";
	private static final String  REMOVE_BY_ID = "delete City where id = ?";
	
	/**
	 * 保存City对象
	 * 
	 * @param city
	 * 			City
	 * @return City
	 * 			City
	 */
	public City saveCity(City city){
		this.getHibernateTemplate().save(city);
		return city;
	}
	
	/**
	 * 更新City对象
	 * 
	 * @param city
	 * 			City
	 * @return City
	 * 			City
	 */
	public City updateCity(City city){
		this.getHibernateTemplate().update(city);
		return city;
	}
	
	/**
	 * 根据ID删除City对象
	 * 
	 * @param id
	 * 			long
	 */
	public void removeCityById(final long id){
		getHibernateTemplate().execute(new HibernateCallback() {
	        public Object doInHibernate(Session s) throws HibernateException, SQLException {
	          Query query = s.createQuery(REMOVE_BY_ID);
	          query.setLong(0, id);
	          query.executeUpdate();
	          return null;
	        }
	      });
	}
	
	/**
	 * 根据ID获取City对象
	 * 
	 * @param id
	 * 			long
	 * @return City
	 * 			City
	 */
	public City findCityById(long id){
		List l = this.getHibernateTemplate().find(LOAD_BY_ID,id);
		if(l == null || l.isEmpty()){
			return null;
		}else{
			return (City) l.get(0);
		}
	}
	
	/**
	 * 根据拼音获取City对象列表
	 * 
	 * @param pinyin
	 * 			String
	 * @param isShow
	 * 			int
	 * @param orderby
	 * @param ascOrDesc
	 * @return List
	 * 			List
	 */
	public List findCitysByCityPinyinYinIsShow(String cityPinyin,int isShow, String orderby, int ascOrDesc){
		StringBuffer sb = new StringBuffer();
		sb.append("from City where 1=1");
		if(StringUtils.isNotBlank(cityPinyin)){
			sb.append(" and(");
			sb.append(" cityAnglic like '%");
			sb.append(cityPinyin);
			sb.append("%'");
			sb.append(" or cityPhonetic like '%");
			sb.append(cityPinyin);
			sb.append("%'");
			sb.append(")");
	    }
		if (isShow != -1) {
			sb.append(" and isShow=");
			sb.append(isShow);
		}
		if (StringUtils.isNotBlank(orderby)) {
			if (ascOrDesc == Constant.ORDER_ASC) {
				sb.append(" order by " + orderby+" asc");
			}
			if (ascOrDesc == Constant.ORDER_DESC) {
				sb.append(" order by " + orderby+" desc");
			}
		}
		final String sql = sb.toString();
		return getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session s) throws HibernateException, SQLException {
				Query query = s.createQuery(sql);
				List list = query.list();
				return list;
			}
		});
	}
	
	/**
	 * 根据参数获取City对象列表
	 * 
	 * @param isShow
	 * 			int
	 * @param orderby
	 * @param ascOrDesc
	 * @return List
	 * 			List
	 */
	public List findCitysByIsShow(int isShow,String orderby,int ascOrDesc){
		StringBuffer sb = new StringBuffer();
		sb.append("from City where 1=1");
		if (isShow != -1) {
			sb.append(" and isShow=");
			sb.append(isShow);
		}
		if (StringUtils.isNotBlank(orderby)) {
			if (ascOrDesc == Constant.ORDER_ASC) {
				sb.append(" order by " + orderby+" asc");
			}
			if (ascOrDesc == Constant.ORDER_DESC) {
				sb.append(" order by " + orderby+" desc");
			}
		}
		final String sql = sb.toString();
		return getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session s) throws HibernateException, SQLException {
				Query query = s.createQuery(sql);
				List list = query.list();
				return list;
			}
		});
	}
	/**
	 * 根据参数获取City对象列表
	 * 
	 * @param isShow
	 * 			int
	 * @param orderby
	 * @param ascOrDesc
	 * @param firstResult
	 * @param maxResults
	 * @return List
	 * 			List
	 */
	public List findCitysByIsShow(int isShow,String orderby,int ascOrDesc,final int firstResult,final int maxResults){
		StringBuffer sb = new StringBuffer();
		sb.append("from City where 1=1");
		if (isShow != -1) {
			sb.append(" and isShow=");
			sb.append(isShow);
		}
		if (StringUtils.isNotBlank(orderby)) {
			if (ascOrDesc == Constant.ORDER_ASC) {
				sb.append(" order by " + orderby+" asc");
			}
			if (ascOrDesc == Constant.ORDER_DESC) {
				sb.append(" order by " + orderby+" desc");
			}
		}
		final String sql = sb.toString();
		return getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session s) throws HibernateException, SQLException {
				Query query = s.createQuery(sql);
				query.setFirstResult(firstResult);
				query.setMaxResults(maxResults);
				List list = query.list();
				return list;
			}
		});
	}
	
	/**
	 * 根据参数获取City对象个数
	 * @param isShow
	 * 			int
	 * @return lonh
	 * 			long
	 */
	public long getCityNumByIsShow(int isShow){
		StringBuffer sb = new StringBuffer();
		sb.append("select count(id) from City where 1=1");
		if (isShow != -1) {
			sb.append(" and isShow=");
			sb.append(isShow);
		}
		List l = this.getHibernateTemplate().find(sb.toString());
		
		if (l == null || l.isEmpty()) {
			return 0;
		} else {
			return (Long) l.get(0);
		}
	}
}
